$(function(){
	var parentHeight,parentWidth;
	parentHeight = $("#processRule-box").height() - 117;
    parentWidth = $("#processRule-box").width();
	$("#processRule-table").jqGrid({
		url: contextPath + "/ProcessRule/queryProcessRuleJqg",
		datatype: "json",
		height: parentHeight,
	    width: parentWidth,
		colNames:['','','','','','规则名称','设备类型','工单类型','项目名称','需上传照片数量','备注信息','操作'],
		colModel:[
			{name:'projectId',hidden:true},
			{name:'workOrderTypeId',hidden:true},
			{name:'deviceTypeId',hidden:true},
			{name:'id',hidden:true},
			{name:'additionalBusinessId',hidden:true},
			{name:'name',index:'name',align:"center"},
			{name:'deviceTypeName',index:'deviceTypeName',align:"center"},
			{name:'workOrderTypeName',index:'workOrderTypeName',align:"center"},
			{name:'projectName',index:'projectName',align:"center"},
			{name:'photoCount',index:'photoCount',formatter:PhC,align:"center"},
			{name:'description',index:'description',align:"center"},
			{name:'moduleId',index:'moduleId',align:"center",formatter: operation,sortalbe:false,width:"100px"}
		],
		sortname:'id',
		sortorder:'asc',
		viewrecords: true,
		rowNum: 20,
		rowList:[5,10,20],
		pager: '#processRule-pager'
	});
	console()
	$("#processRule-table").jqGrid('navGrid', '#processRule-pager', {edit : false,add : false,del : false,search: false});
});
//查询产品线
$.ajax({
    url: contextPath + "/WorkOrderType/queryAllWorkOrderType",
    type: "get",
    cache: false,
    dataType: "json",
    success: function(data){
        var str = '<option value="">全部</option>';
        for (var i =0 ,len = data.length; i < len; i++) {
        	str += '<option value="' + data[i].id + '">' + data[i].name + "</option>";
        }
        $("#workOrderTypeId").html(str);
    },
    error: function(data){
        console.log(data);
        $("#workOrderTypeId").html('<option value="">数据异常</option>');
    }
});
// 查询设备类型
$.ajax({
    url: contextPath + "/DeviceType/queryAllDeviceType",
    type: "get",
    cache: false,
    dataType: "json",
    success: function(data){
        var str = '<option value="">全部</option>';
        for (var i =0 ,len = data.length; i < len; i++) {
        	str += '<option value="' + data[i].id + '">' + data[i].name + "</option>";
        }
        $("#deviceTypeId").html(str);
    },
    error: function(data){
        console.log(data);
        $("#deviceTypeId").html('<option value="">数据异常</option>');
    }
});
//查询按钮
$("#query").on("click",function(){
    doQuery();
});

function doQuery() {
    $("#processRule-table").setGridParam({
        url: contextPath + "/ProcessRule/queryProcessRuleJqg",
        postData: {
            queryCond: {
                'name': $('#processRuleName').val(),
                'deviceTypeId': $('#deviceTypeId').val() ? Number($('#deviceTypeId').val()) : null,
                'workOrderTypeId': $('#workOrderTypeId').val() ? Number($('#workOrderTypeId').val()) : null
            }
        },
        page: 1
    }).trigger("reloadGrid");
}

function autodivheight(){
    parentHeight = $("#processRule-box").height() - 117;
    parentWidth = $("#processRule-box").width();
}
function operation(cellvalue,options,rowObject){
	var str = "";
        str += "<input style='margin:5px 3px' class='btn btn-primary btn-xs copy' type='button' value='拷贝新建'>";
	// if([[${#authorization.expression('hasAuthority(''modifyAuthority'')')}]]){
		str += "<input style='margin:5px 3px' class='btn btn-primary btn-xs modify' type='button' value='修改'>";
	// }
	// if([[${#authorization.expression('hasAuthority(''deleteAuthority'')')}]]){
		str += "<input style='margin:5px 3px' class='btn btn-danger btn-xs del' type='button' value='删除' data-toggle='modal' data-target='.bs-example-modal-sm'>";
	// }
    return str;
}
function PhC(cellvalue,options,rowObject){
	var Phcount = "";
	if(cellvalue == 0){
		Phcount ="任意";
		return Phcount;
	}else{
		return cellvalue;
	}
}
//修改
var processRuleData = null;
/* 修改 || 删除 */
$("#processRule-table").on('click','.modify,.del,.copy',function(){
	var id = $(this).parent().parent().attr('id');
	processRuleData = $("#processRule-table").jqGrid('getRowData',id);
    delete processRuleData.operate;
    if($(this).hasClass("modify")){
   		window.location.hash = "qualitycontrol-systemGroup-modifyProcessRule";
    }else {
        if ($(this).hasClass("copy")) {
            window.location.hash = "qualitycontrol-systemGroup-copyProcessRule";
        } else {
        }
        console.log(processRuleData);
    }
});
$("#deleteData").on('click',function(){
	$.ajax({
        url:contextPath + '/ProcessRule/deleteProcessRule/'+ processRuleData.id,
        type:"delete",
        contentType:"application/json",
        dataType:"json",
        data: JSON.stringify(processRuleData),
        success:function(data){
            if(data.code == 1){
                $('#deleteNotice').modal('hide');
                toastr.success('删除成功！');
                processRuleData = null;
            }else{
                toastr.error(data.message,'删除失败！');
            }
            doQuery();
        },
        error:function(data){
        	console.log(data);
            toastr.error('操作失败，请稍后再试！');
        }
    });
});
$(window).resize(function(){
	$("#processRule-table").setGridWidth($('#processRule-box').width());
	$("#processRule-table").setGridHeight($('#processRule-box').height() - 117);
});　
//侧边栏缩小时，实时的给jqgrid的宽和高赋值
$("#asidebarSwitch").on('click',function() {
    var time = 0;
    var set = setInterval(function (){
        $("#processRule-table").setGridWidth($('#processRule-box').width());　　
        $("#processRule-table").setGridHeight($('#processRule-box').height() - 83);
        time++;
        if(time >= 200){
            clearInterval(set);
        }
    },time);
});　